<?php 

namespace Weixin\Model;

use Common\Model\Model;

class UserModel extends Model
{
    
    /**
     * 获取用户信息
     * @param unknown $userid
     * @return unknown
     */
    public function getUserInfo($userid)
    {
        $data = M('User')->where(array('userid' => $userid))->find();
        return $data;
    }
    
    /**
     * 获取发布成功导师专栏数
     * @param unknown $userid
     * @return unknown
     */
    public function getTeachNum($userid)
    {
        $num = M('Teachlist')->where(array('userid' => $userid, 'status' => 1))->count();
        return $num;
    }
    
    /**
     * 获取发布的食谱数
     * @param unknown $userid
     * @return unknown
     */
    public function getWorkNum($userid)
    {
        $num = M('Works')->where(array('userid' => $userid, 'type' => 1))->count();
        return $num;
    }
    
    /**
     * 获取随拍数
     * @param unknown $userid
     * @return unknown
     */
    public function getSuipaiNum($userid)
    {
        $num = M('Suipai')->where(array('userid' => $userid))->count();
        return $num;
    }
    
    /**
     * 随拍列表
     * @param unknown $userid
     * @return string
     */
    public function getSpList($userid, $firstRow, $listRows)
    {
        
        $list = $this
        ->table(array(
            C('DB_PREFIX').'suipai'=>'S',
            C('DB_PREFIX').'user'=>'U',
        )
        )
        ->where('S.userid=U.userid and S.userid='.$userid)
        ->field('S.title,S.msgnum,S.content,S.id,S.create_time,S.image,S.all_pic,U.nickname,U.uavatar,U.openuid,U.userid,U.ifrz')
        ->order('S.create_time DESC')
        ->limit($firstRow . ',' . $listRows)
        ->select();
    
    
        foreach($list as $key => $value){
            $list[$key]['create_time'] = format_time($value['create_time'], 'Y-m-d H-i');
            $list[$key]['uavatar'] = getUavatar($value['uavatar']);
            $list[$key]['image'] = WEB_URL.$value['image'];
    
            $yf_thumb = array();
            $yf_pic = array();
    
            if(!empty($value['all_pic']))
            {
                $yf_arr = json_decode($value['all_pic'], true);
                $yf_temp = array();
                foreach($yf_arr as $k => $v)
                {
                    //$yf_thumb[] = WEB_URL.$v;
                    $yf_ex = explode("/", $v);
                    $yf_ex[count($yf_ex)-1] = str_replace("yf_", "", $yf_ex[count($yf_ex)-1]);
                    $yf_pic[] = $temp = WEB_URL.implode("/", $yf_ex);
                    $yf_thumb[] = array(
                        'thumb' => $v,
                        'pic' => $temp
                    );
                }
            }
            else
            {
                $yf_arr = array();
                //$yf_thumb[] = $yf_pic[] = WEB_URL.$value['image'];
                $yf_thumb[] = array(
                    'thumb' => $value['image'],
                    'pic' => WEB_URL.$value['image']
                );
                $yf_pic[] = WEB_URL.$value['image'];
            }
             
            $list[$key]['thumb']=$yf_thumb;
             
            $list[$key]['pic'] = implode(',', $yf_pic);
        }
    
        return $list;
    }
    
    /**
     * 食谱列表
     */
    public function getWorkList($userid, $first, $last)
    {
        $Model=new Model();
        $list=$Model
        ->table(array(
            C('DB_PREFIX').'works'=>'W',
        )
        )
        ->where('W.type=1 and W.userid='.$userid)
        ->field('W.title,W.content,W.workid,W.status,W.image,W.msgnum,W.goodnum')
        ->order('W.create_time DESC')
        ->limit($first . ',' . $last)
        ->select();
        if($list)
        {
            foreach($list as $k => $v)
            {
                $list[$k]['image'] = WEB_URL.$v['image'];
                $list[$k]['title'] = hy_substr($v['title'], 15);
               // $list[$k]['content'] = hy_substr($v['content'], 14);
                $list[$k]['status'] = '已通过';
                if($v['status'] == 0)
                {
                    $list[$k]['status'] = '待审核';
                }
                elseif($v['status'] == 2)
                {
                    $list[$k]['status'] = '未通过';
                }
            }
        }
        return $list;
    }
    
    /**
     * 获取导师列表
     * @param unknown $userid
     * @param unknown $first
     * @param unknown $last
     * @return Ambigous <unknown, string>
     */
    public function getTeachList($userid, $first, $last)
    {
        $Model=new Model();
        $list=$Model
        ->table(array(
            C('DB_PREFIX').'user'=>'U',
            C('DB_PREFIX').'teachlist'=>'T'
        )
        )
        ->where('U.userid=T.userid and T.status=1 and T.userid='.$userid)
        ->field('T.teachid, T.title, T.content, T.create_time, T.iflight, T.ifimg, T.msgnum, U.nickname')
        ->order('T.create_time DESC')
        ->limit($first . ',' . $last)
        ->select();
        if($list)
        {
            foreach($list as $k => $v)
            {
                $list[$k]['create_time'] = format_time($v['create_time'], 'Y-m-d H:i');
                $list[$k]['title'] = hy_substr($v['title'], 15);
                $nickname = M('User')->where(array('userid' => $v['userid']))->getField('nickname');
                $list[$k]['nickname'] = hy_substr($nickname, 8);
                if($v['ifimg'])
                {
                    preg_match('/<\s*img\s+[^>]*?src\s*=\s*(\'|\")(.*?)\\1[^>]*?\/?\s*>/i', htmlspecialchars_decode($v['content']), $match);
                    if($match[0])
                    {
                        $list[$k]['image'] = $match[2];
                    }
                }
                $list[$k]['content'] = hy_substr(strip_tags(htmlspecialchars_decode($v['content'])), 30);
            }
        }
        return $list;
    }
}